package com.cwl.pulgins;

import com.cwl.pulgins.domain.Column;
import com.cwl.pulgins.domain.Table;
import com.cwl.pulgins.handle.Class2TableConverter;
import com.cwl.pulgins.handle.SqlGenerator;
import com.cwl.pulgins.utils.LogUtils;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import org.codehaus.jettison.json.JSONObject;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @program: pulgin
 * @description:
 * @author: ChenWenLong
 * @create: 2019-09-03 16:00
 **/
public class CreateTableGenerateAction extends AnAction {

    private Class2TableConverter class2TableConverter = new Class2TableConverter();
    private SqlGenerator sqlGenerator = new SqlGenerator();

    public void actionPerformed(AnActionEvent anActionEvent) {
        try {
            File file = new File("D:\\create_table.vm");
            if(!file.exists()){
                createFile();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        PsiElement data = DataKeys.PSI_ELEMENT.getData(anActionEvent.getDataContext());
        if (!(data instanceof PsiClass)){
            LogUtils.showError("必须选择一个类");
            return;
        }
        Table table = class2TableConverter.convert((PsiClass) data);
        String createSql = sqlGenerator.generate(table);
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(createSql);
        LogUtils.showInfo("生成成功,sql已复制到剪切板");
        clip.setContents(tText, null);
    }

    /**
     * 功能描述:
     * 〈在本地创建一个可执行文件〉
     *
     * @params : []
     * @return : void
     * @author : cwl
     * @date : 2019/9/4 15:04
     */
    public static void createFile() throws IOException {
        File file = new File("D:\\create_table.vm");
        FileWriter writer = null;
        try {
            file.createNewFile();
            writer = new FileWriter(file, true);
            writer.append("CREATE TABLE `${table.name}` (\n" +
                    "#foreach( $column in $table.columns )\n" +
                    "`${column.name}` ${column.type} " +
                    "#if($column.isNull) $column.isNull #end " +
                    "#if($column.encoding) $column.encoding #end " +
                    "#if($column.unsigned) $column.unsigned #end "+
                    "#if($column.comment)COMMENT '$column.comment'#end,\n " +
                    "PRIMARY KEY (`id`)\n " +
                    "#if($column.index) $column.index #end,#end " +
                    ") ENGINE=InnoDB ;");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != writer)
                writer.close();
        }
    }
}